package fts.ftsui;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fts.ftscontrol.Facade;
import fts.ftsmonitoring.InitializationStatus;
import fts.ftsmonitoring.initializer.IInitializerObserver;


public class InitializerServlet extends HttpServlet implements IInitializerObserver
{

	Facade facade;
	static InitializationStatus initStatus;
	static int initProgress = 0;
	
	public InitializerServlet(Facade facade)
	{
		this.facade = facade;
		this.facade.addInitializerObserver(this);
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		super.service(arg0, arg1);
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		if("start".equals(req.getParameter("action")))
		{
			facade.startInitializerThread();
		}
		else if("stop".equals(req.getParameter("action")))
		{
			facade.stopInitializerThread();
		}
		else if("status".equals(req.getParameter("action")))
		{
			int progVal = initProgress;
			String progress = "";
			for(int i=0; i<progVal; i++)
			{
				progress += "<td style='background-color:green;'><div style='width:2px; height:10px; background-color:green;'></div></td>";
			}
			for(int i=progVal; i<100; i++)
			{
				progress += "<td style='background-color:white;'><div style='width:2px; height:10px; background-color:white;'></div></td>";
			}
				String statusHtml = "<html><head><script language='JavaScript'>var time = null; function move() {window.location = 'http://localhost:8181/init?action=status'} timer=setTimeout('move()',500);</script></head><body>"
						+ "<table border='0' style='border: black 1px solid;'><tr style='height:10px;'>" + progress + "</tr></table>"
						+ "</body></html>";
			resp.getWriter().println(statusHtml);
	        resp.getWriter().flush();
	        return;
		}
        String html = "<html><head><title>Initialization</title></head><body>" 
        	+ "<h1>Initialization</h1>"
        	+ "<a href=\"http://localhost:8181/init\">Refresh</a><br/><br/>"
        	+ "Status: " + initStatus + "<br/><br/>"
        	+ "<a href=\"http://localhost:8181/init?action=start\">Start</a><br/><br/>"
        	+ "<a href=\"http://localhost:8181/init?action=stop\">Stop</a><br/><br/>"
        	+ "<iframe style='width:630px; height:70px; border: 0px solid #ffffff;' frameborder='0' src='http://localhost:8181/init?action=status'></iframe></body></html>";
        resp.getWriter().println(html);
        resp.getWriter().flush();
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
	public void onInitializerProgressChange(int progress)
	{
		initProgress = progress;
	}
	public void onInitializerStatusChange(InitializationStatus status)
	{
		initStatus = status;
	}
}
